﻿using SGA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace SGA.Mailing.MovementsMailing
{
    public partial class MovementsMailing : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void LoadContact()
        {
            using (var bd = new MailingEntities())
            {
                SupporterContacts qrySuppCts = (from supporterContacts in bd.SupporterContacts
                                                where supporterContacts.IdSupporters.ToString() == IdSupportersTbox.Text
                                                      && supporterContacts.IdContactTypes.ToString() == ContactTypesDdl.SelectedValue
                                                select supporterContacts).SingleOrDefault();
                if (qrySuppCts != null)
                {
                    ContacTbox.Text = qrySuppCts.Designation;
                }
                else
                {
                    ContacTbox.Text = string.Empty;
                }
            }
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            using (var bd = new MailingEntities())
            {
                SGA.Model.Supporters qrySupp = (from supporters in bd.Supporters
                                                where supporters.Id.ToString() == IdSupportersTbox.Text
                                                select supporters).SingleOrDefault();
                if (qrySupp != null)
                {
                    IdSupportersTbox.Text = qrySupp.Id.ToString();
                    NameTbox.Text = qrySupp.Designation;
                    AddressTbox.Text = qrySupp.Address;
                    ComplementAddressTbox.Text = qrySupp.ComplementAddress;
                    PostalCode4Tbox.Text = qrySupp.PostalCodeLocal;
                    PostalCode3Tbox.Text = qrySupp.PostalCodeStreet;
                    LocalLbl.Text = "CARREGAR A BD DOS CÓDIGOS POSTAIS!";
                    if (qrySupp.WantIRS != null)
                        WantIRSCheckBox.Checked = (bool)qrySupp.WantIRS;
                    LoadContact();
                }

                //BirthdayLetters qryBday = (from BirthdayLetters in bd.BirthdayLetters
                //                           where BirthdayLetters.IdSupporters.ToString() == IdSupportersTbox.Text
                //                           select BirthdayLetters).SingleOrDefault();
                //if (qryBday != null)
                //{
                //    //if (qryBday.WantBirthdayLetter != null)
                //    //    BirthdayCheckBox.Checked = (bool)qryBday.WantBirthdayLetter;

                //    BirthdayDayTbox.Text = qryBday.BirthdayDay.ToString();
                //    BirthdayMonthTbox.Text = qryBday.BirthdayMonth.ToString();
                //    BirthdayYearTbox.Text = qryBday.BirthdayYear.ToString();
                //}
                LoadMailings();
            }
        }

        protected void ContactTypesDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadContact();
        }

        /* carrega todos os envios feitos ao supporter */
        protected void LoadMailings()
        {
            int idSupporter = int.Parse(IdSupportersTbox.Text);

            using (MailingEntities bd = new MailingEntities())
            {
                var qryMailings = bd.ResponsesToMailings(idSupporter);
                MailingLvw.DataSource = qryMailings.ToList();
                MailingLvw.DataBind();
            }
        }

    }
}